﻿using BllLog;
using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace SharpBrowser
{
    //static class Program
    //{
    //    /// <summary>
    //    /// The main entry point for the application.
    //    /// </summary>
    //    [STAThread]
    //    static void Main()
    //    {

    //        // ***this line is added***
    //        if (Environment.OSVersion.Version.Major >= 6)
    //            SetProcessDPIAware();

    //        Application.EnableVisualStyles();
    //        Application.SetCompatibleTextRenderingDefault(false);

    //        //暂时没起作用（没有找到Logs文件夹），原因未知
    //        BllLog.LogHelper.AppConfigure();
    //        LogHelper.Info("启动");

    //        Application.Run(new MainForm());
    //    }

    //    // ***also dllimport of that function***
    //    [System.Runtime.InteropServices.DllImport("user32.dll")]
    //    private static extern bool SetProcessDPIAware();

    //}




    static class Program
    {
        [STAThread]
        static void Main()
        {
            // 添加异常处理
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new MainForm());




            // ***this line is added***
            if (Environment.OSVersion.Version.Major >= 6)
                SetProcessDPIAware();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //暂时没起作用（没有找到Logs文件夹），原因未知
            BllLog.LogHelper.AppConfigure();
            LogHelper.Info("启动");

            Application.Run(new MainForm());
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show($"线程异常: {e.Exception.Message}\n{e.Exception.StackTrace}", "错误");
            // 记录日志
            LogException(e.Exception);
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            MessageBox.Show($"未处理异常: {ex?.Message}\n{ex?.StackTrace}", "错误");
            LogException(ex);
        }

        private static void LogException(Exception ex)
        {
            // 记录到文件
            string log = $"[{DateTime.Now}] 异常: {ex?.Message}\n堆栈: {ex?.StackTrace}\n";
            
            System.IO.File.AppendAllText("error_log.txt", log);
        }

        // ***also dllimport of that function***
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();
    }
}



